﻿using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AfvandingService
{
    public class Pumpestation : IPumpestation
    {
        #region Fields

        private const string CONNECTIONSTRING = "Data Source=10.165.150.52;Database=Afvandingsprojekt;Integrated Security=False;User ID=dm122;Password=dm122;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False";
        private int _pumpestationsnr;
        private string _netværksadresse;
        private string _ip;
        private int _port;
        private double _vandstand;
        private string _vandstandsrisiko;
        private List<Pumpe> _pumpeliste;
        private object _lock = new object();

        #endregion

        #region ctor

        // skal bruges til serialization
        public Pumpestation()
        {
            _pumpeliste = new List<Pumpe>();
            _pumpeliste = HentPumper();
        }

        public Pumpestation(int pumpestationsnr, string netværksadresse, string ip, int port, string vandstandsrisiko, double vandstand)
        {
            _pumpestationsnr = pumpestationsnr;
            _netværksadresse = netværksadresse;
            _ip = ip;
            _port = port;
            _vandstand = vandstand;
            _vandstandsrisiko = vandstandsrisiko;
        }

        #endregion

        #region Methods

        public string AnmodningVandstand()
        {
            return "Vandstanden på pumpestation med netværksadressen: " + _netværksadresse + " er " + _vandstand + " mm";
        }

        public void AnmodningStartPumpe(int pumpenr, Pumpestation pumpestation)
        {
            foreach (var pumpe in _pumpeliste)
            {
                if (pumpe.Pumpenr == pumpenr && pumpe.PumpestationsNr == pumpestation.Pumpestationsnr)
                {
                    Thread traad = new Thread(() => pumpe.Start(pumpestation));
                    traad.Start();
                }
            }
        }

        public void AnmodningStopPumpe(int pumpenr, Pumpestation pumpestation)
        {
            foreach (var pumpe in _pumpeliste)
            {
                if (pumpe.Pumpenr == pumpenr && pumpe.PumpestationsNr == pumpestation.Pumpestationsnr)
                {
                    pumpe.Stop();
                }
            }
            // True = Stoppet 
            // False = Ikke stoppet
        }


        public void OpretPumpe(double pumpekapacitet, int pumpenr, int pumpestationsnr)
        {
            Pumpe pumpe = new Pumpe(pumpekapacitet, pumpenr, pumpestationsnr);
            _pumpeliste.Add(pumpe);
        }

        public void SletPumpe(Pumpe pumpe)
        {
            _pumpeliste.Remove(pumpe);
        }

        public void RetPumpe(Pumpe pumpe, double pumpekapacitet, int pumpenr)
        {
            foreach (var pump in _pumpeliste)
            {
                if (pumpe == pump)
                {
                    pump.Pumpekapacitet = pumpekapacitet;
                    pump.Pumpenr = pumpenr;
                }
            }
        }

        public List<Pumpe> HentPumper()
        {
            List<Pumpe> TEMPpumper = new List<Pumpe>();

            using (SqlConnection conn = new SqlConnection(CONNECTIONSTRING))
            {
                SqlCommand cmd = new SqlCommand("SP_HentPumper", conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    Pumpe TEMPpumpe = new Pumpe();

                    TEMPpumpe.Pumpekapacitet = float.Parse(reader["Pumpekapacitet"].ToString());
                    TEMPpumpe.Pumpenr = (int)reader["Pumpenr"];
                    TEMPpumpe.Status = (bool)reader["PumpeStatus"];
                    TEMPpumpe.PumpestationsNr = (int) reader["PumpestationsNr"];
                         

                    TEMPpumper.Add(TEMPpumpe);
                }
            }
            return TEMPpumper;
        }

        public List<Pumpe> Pumper(int pumpestationsnr)
        {
            List<Pumpe> pumpeliste = new List<Pumpe>();
            foreach (var pumpe in _pumpeliste)
            {
                if (pumpe.PumpestationsNr == pumpestationsnr)
                {
                    pumpeliste.Add(pumpe);
                }
            }
            return pumpeliste;
        }

        #endregion

        #region Properties

        public List<Pumpe> Pumpeliste { get { return _pumpeliste; } set { _pumpeliste = value;} }

        public int Pumpestationsnr { get { return _pumpestationsnr; } set { _pumpestationsnr = value; } }

        public string Netværksadresse { get { return _netværksadresse; } set { _netværksadresse = value; } }

        public string IP { get { return _ip; } set { _ip = value; } }

        public int Port { get { return _port; } set { _port = value; } }

        public string Vandstandsrisiko { get { return _vandstandsrisiko; } set { _vandstandsrisiko = value; } }

        public double Vandstand
        {
            get 
            {
                lock (_lock)
                {
                    return _vandstand;
                }
            }
            set
            {
                lock (_lock)
                {
                    _vandstand = value; 
                }
            }
        }

        public List<Pumpe> PumpeListe { get { return _pumpeliste; }} 

    

        #endregion
    }
}
